package com.orangelife.mobile.parkingcard.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.curry.android.http.OrangeDataHandler;
import com.curry.android.http.OrangeHttpBiz;
import com.curry.android.http.VolleyHttpUtil;
import com.curry.android.util.DialogHelper;
import com.curry.android.util.IntentHelper;
import com.curry.orangelife.mobile.R;
import com.google.gson.reflect.TypeToken;
import com.markupartist.android.widget.PullToRefreshListView;
import com.orangelife.mobile.common.activity.PullRefreshBaseActivity3;
import com.orangelife.mobile.common.view.EmptyLayout;
import com.orangelife.mobile.parkingcard.adapter.ParkingSpaceUsersAdapter;
import com.orangelife.mobile.parkingcard.bean.ParkingListBean;
import com.orangelife.mobile.parkingcard.bean.ParkingUsersListBean;
import com.orangelife.mobile.parkingcard.biz.ParkingSpaceHttpBiz;


import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Handler;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 停车位的使用者列表界面
 * Created by HeYuCan on 2016/7/7.
 */
public class ParkingSpaceUsersActivity extends PullRefreshBaseActivity3 {


    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.emptyLayout)
    EmptyLayout emptyLayout;
    @Bind(R.id.tv_parking_info)
    TextView tvParkingInfo;
    @Bind(R.id.btn_authorize_another_to_use)
    Button btnAuthorization;
    @Bind(R.id.plvList)
    PullToRefreshListView pullToRefreshListView;

    private String apartmentId;
    private String linkAptHouseholderID;
    //    private String ownerID;
    private List<ParkingUsersListBean> mList = new ArrayList<>();
    private ParkingSpaceUsersAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_parking_space_users);
        ButterKnife.bind(this);
        getIntentInfo();
        getAdapter();

        setListener();
        initTitle();
        setXListViewAndEmptyLayoutListener(pullToRefreshListView, emptyLayout, adapter);
        requestList();
    }

    @Override
    protected boolean isDelayRequest() {
        return true;
    }

    private void getIntentInfo() {
        Intent intent = getIntent();
        String houseInfo = intent.getStringExtra("houseInfo");
        apartmentId = intent.getStringExtra("apartmentID");
        linkAptHouseholderID = intent.getStringExtra("linkAptHouseholderID");
        tvParkingInfo.setText(houseInfo);
    }


    private ParkingSpaceUsersAdapter getAdapter() {
        if (adapter == null) {
            adapter = new ParkingSpaceUsersAdapter(this);
        }
        return adapter;
    }

    private void setListener() {
        emptyLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                ???showProgress没有效果
                showProgress();
                requestList();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        showProgress();
        ParkingSpaceHttpBiz.requestParkingSpaceUsersList1(apartmentId, handler);
    }

    private android.os.Handler handler = new android.os.Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    try {
                        JSONObject response = new JSONObject(String.valueOf(msg.obj));
                        List<ParkingUsersListBean> list = OrangeDataHandler.getListFromJsonWithPageEntity(msg.obj, new TypeToken<List<ParkingUsersListBean>>() {
                        }.getType(), null, null);
                        handleAfterRequest(isConnected(), OrangeHttpBiz.getTotalNum(response), list, pullToRefreshListView, adapter);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
            }
        }
    };

    @OnClick({R.id.btn_authorize_another_to_use})
    public void onClick(View view) {
        IntentHelper.getInstances().intentWithExtraString(this, GrantPermissionActivity.class, "linkAptHouseholderID", linkAptHouseholderID);
    }

    private void initTitle() {
        tvTitle.setText("使用授权明细");
    }


    @Override
    protected EmptyLayout getEmptyLayout() {
        return emptyLayout;
    }

    @Override
    protected List getData() {
        return mList;
    }

    @Override
    protected int getDataCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    protected void requestWhenPullRefresh() {
        requestList();
    }


    private void requestList() {
        ParkingSpaceHttpBiz.requestParkingSpaceUsersList(apartmentId, pageSize, pageNo, new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                dismissProgress();
                if (response != null) {
                    List<ParkingUsersListBean> list = ParkingSpaceHttpBiz.handleParkingSpaceUsersList(response);
                    if (list == null || list.size() == 0) {
                        emptyLayout.setErrorType(EmptyLayout.NODATA);
                        emptyLayout.setErrorImage(R.drawable.no_data);
                    } else {
                        handleAfterRequest(isConnected(), OrangeHttpBiz.getTotalNum(response), list, pullToRefreshListView, getAdapter());
                    }
                }
            }
        });
    }


}
